package com.ruijie.acl.web.validator;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.apache.commons.lang.StringUtils;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.faces.Validator;

import com.ruijie.acl.service.TemplateService;
import com.ruijie.foundation.international.MessageSupport;
/**
 * <p>
 * Title: 模板名重名验证
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * 验证模板名称名字是否重复
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-07-14
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * @author ASAI
 */
@Name("templateNameValidator")
@Validator
public class TemplateNameValidator  extends MessageSupport implements
javax.faces.validator.Validator{
	@In("#{templateService}")
	private TemplateService templateService;
	@In private Map<String, String> messages;

	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
		String name=value.toString().trim();
		if(component.getAttributes().get("required").toString().equals("true")){
			if(!StringUtils.isNotEmpty(name.replaceAll("　",   " ").trim())){
				throw new ValidatorException(new FacesMessage(messages.get("javax.faces.component.UIInput.REQUIRED")));
			}
		}
		Long id = (Long)component.getAttributes().get("templateId");
		boolean isExisted=this.templateService.isExistsTemplateName(name, id);
		if(isExisted){
			throw new ValidatorException(new FacesMessage(
					getI18NMessage("acl_template_validation_templateNameExisted")));
		}
	}
}
